package com.wise.service.portal.content.productindustry.product;

import com.wise.entities.content.Product;
import com.wise.framework.dao.SqlSelect;
import com.wise.framework.daomodel.DaoList;
import com.wise.framework.validation.Precondition;
import com.wise.summer.v1.portal.content.productindustrydetail.GetProductIndustryDetail;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import java.util.List;

import static com.wise.model.content.ContentErrMsgs.Err_001000250009;
import static com.wise.model.content.ContentErrMsgs.Err_002000250009;

/**
 * @author shaoming
 * @date 2019-09-09
 */
@Service
@SummerService
public class GetProductIndustryByRouteIdService implements SummerServiceBean<GetProductIndustryDetail> {

    @Override
    public void sum(GetProductIndustryDetail summer) {
        SqlSelect sqlSelect = SqlSelect.selec("*")
                .fromClassName(Product.class)
                .eq(Product._categoryType, 1);

        List<Product> products = DaoList.s(Product.class, sqlSelect);
        GetProductIndustryDetail.Result result = new GetProductIndustryDetail.Result();
        boolean flag = false;
        for (Product product : products) {
            if (summer.getRouteId().equals(product.getRouteId())) {
                BeanUtils.copyProperties(product, result);
                flag = true;
                break;
            }
        }
        if (!flag) {
            for (Product product : products) {
                if (product.getId().equals(summer.getRouteId())) {
                    BeanUtils.copyProperties(product, result);
                    break;
                }
            }
        }

        Precondition.checkNotNull(result, Err_002000250009);

        summer.setSummerResult(result);
    }
}
